
import { Collapse } from 'antd-mobile'
import './index.scss'
import { useMemo, useState } from 'react'
import dayjs from 'dayjs'
import _ from 'loadsh'

const Panel = ({list,date})=> {
    const dayResult = useMemo(()=> {
        // 收入/支出/结余
        const pay = (list && list.filter(d => d.type === 'pay').reduce((a,c)=> a+c.money,0)) || 0
        const income = (list && list.filter(d => d.type === 'income').reduce((a,c)=> a+c.money,0))|| 0
        return {
            pay,
            income,
            total:  income - pay
        }
    },[list])
    return (
        <div className="myItem">
            <div className="myDate">
                {date}月
            </div>
            <div className="myPanel">
                <div className="panelItem">
                    <span className="payTitle">支出</span>
                    <span className="money">{dayResult.pay.toFixed(2)}</span>
                </div>
                <div className="panelItem">
                    <span className="incomeTitle">收入</span>
                    <span className="money">{dayResult.income.toFixed(2)}</span>
                </div>
                <div className="panelItem">
                    <span className="totalTitle">结余</span>
                    <span className="money totalMoney">{dayResult.total.toFixed(2)}</span>
                </div>
            </div>
        </div>
    )
}
const DayBill = ({list}) => {
    // 按日期分组数据
    const yearGroup = useMemo(()=> {
        const groupData = _.groupBy(list,(item)=> dayjs(item.date).format('MM'))
        const yearKeys = Object.keys(groupData)
        return {
            groupData,
            yearKeys
        }
    },[list])
    console.log("yearGroup:",yearGroup)

    return (
        <div className="yearBill">
            {yearGroup.yearKeys.map(key => (
                <Panel date={key} list={yearGroup.groupData[key]}/>
            ))}
        </div>
    )
}

export default DayBill